package models

import (
	"centerServer/dbhelper"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	"time"
)

func init() {
	orm.RegisterModelWithPrefix("center_", new(BonusLingQianDataModel), new(UserShareInfo), new(UserWxInfo),
		new(RecoderGold), new(RecoderStars))
}

// 请求数据记录
type BonusLingQianRequestDataModel struct {
}

func (u *BonusLingQianRequestDataModel) TableName() string {
	return "lingqianrequest"
}

// 发放数据记录
type BonusLingQianDataModel struct {
	Id             int64
	RE_MchAppId    string    `orm:"column(ReMchappid)"`       //  appid
	RE_OpenId      string    `orm:"column(Reopenid)"`         //  用户openid
	RE_Amount      int       `orm:"column(ReAmount)"`         // 	发放的钱数目(单位:分,最少30)
	RE_RequestTime int64     `orm:"column(ReRetime)"`         //  申请时间戳(超过30秒订单不会支付)
	RE_ReBillno    string    `orm:"column(ReRebillno);index"` //  申请订单号(随机32位字符串)
	RE_Sign        string    `orm:"column(ReSign)"`           //  签名
	Appid          string    `orm:"column(Appid)"`
	Mch_id         string    `orm:"column(Mchid)"`
	Nonce_str      string    `orm:"column(NonceStr)"`
	Mch_billno     string    `orm:"column(MchBillno)"`
	Openid         string    `orm:"column(Openid)"`
	CheckName      string    `orm:"column(CheckName)"`
	Amount         string    `orm:"column(Amount)"`
	Desc           string    `orm:"column(Desc)"`
	Sign           string    `orm:"column(Sign)"`
	IsSuccess      string    `orm:"column(IsSuccess)"`
	Created        time.Time `orm:"auto_now_add;type(datetime)"`
}

func (this *BonusLingQianDataModel) TableName() string {
	return "lingqian"
}

// 填入申请数据
func (this *BonusLingQianDataModel) FillRequestData(rData *LingQianRequest) {
	this.RE_Amount = rData.Amount
	this.RE_MchAppId = rData.MchAppId
	this.RE_OpenId = rData.OpenId
	this.RE_ReBillno = rData.ReBillno
	this.RE_RequestTime = rData.RequestTime
	this.RE_Sign = rData.Sign
}

// 填入微信零钱发送数据
func (this *BonusLingQianDataModel) FillLinqQianRequestData(lqrData *BonusLingQian) {
	this.Appid = lqrData.Appid.Text
	this.Mch_id = lqrData.Mch_id.Text
	this.Nonce_str = lqrData.Nonce_str.Text
	this.Mch_billno = lqrData.Mch_billno.Text
	this.Openid = lqrData.Openid.Text
	this.CheckName = lqrData.CheckName.Text
	this.Amount = lqrData.Amount.Text
	this.Desc = lqrData.Desc.Text
	this.Sign = lqrData.Sign.Text
}

// 设置是否成功
func (this *BonusLingQianDataModel) SetSuccess(isSuccess bool) {
	if isSuccess {
		this.IsSuccess = "1"
	} else {
		this.IsSuccess = "0"
	}
}

// 写入数据库
func (this *BonusLingQianDataModel) WriteToDB() {
	opr := dbhelper.GetMysqlHelper().GetNewOrm()
	findData := BonusLingQianDataModel{RE_ReBillno: this.RE_ReBillno}
	err := opr.Read(&findData, "ReRebillno")
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		// 新增
		id, errI := opr.Insert(this)
		if errI == nil {
			this.Id = id
		} else {
			logs.Error("Insert BonusLingQianData err :" + errI.Error())
		}
	} else {
		// 更新
		if _, errU := opr.Update(this); err != nil {
			logs.Error("Update BonusLingQianData err :" + errU.Error())
		}
	}
}

// 查询是否已经存在
func (this *BonusLingQianDataModel) IsExsit() bool {
	opr := dbhelper.GetMysqlHelper().GetNewOrm()
	findData := BonusLingQianDataModel{RE_ReBillno: this.RE_ReBillno}
	err := opr.Read(&findData, "ReRebillno")
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return false
	}
	return true
}


// 用户分享的下线数据
type UserShareInfo struct {
	UnionId 	string    	`orm:"column(UnionId);pk"`       	//  用户唯一ID,数据主键
	InviteCode 	string 		`orm:"column(InviteCode)"`				// 	上线邀请码
}

func (this *UserShareInfo) TableName() string {
	return "UserShare"
}

// 写入数据库
func (this *UserShareInfo) WriteToDB() {
	opr := dbhelper.GetMysqlHelper().GetNewOrm()
	findData := UserShareInfo{UnionId: this.UnionId}
	err := opr.Read(&findData, "UnionId")
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		// 新增
		_, errI := opr.Insert(this)
		if errI != nil {
			logs.Error("Insert UserShareInfo err :" + errI.Error())
		}
	}
}

// 查询上级
func (this *UserShareInfo) FindInviteCode() string {
	opr := dbhelper.GetMysqlHelper().GetNewOrm()
	findData := UserShareInfo{UnionId: this.UnionId}
	err := opr.Read(&findData, "UnionId")
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return ""
	}
	return findData.InviteCode
}


// 微信用户数据保存
type UserWxInfo struct {
	OpenId     		string `orm:"column(OpenId);pk"`
	UnionId			string `orm:"column(UnionId)"`
	NickName		string	`orm:"column(NickName)"`
	Sex 			int     `orm:"column(Sex)"`
	HeadImgUrl      string  `orm:"column(HeadImgUrl)"`
}

func (this *UserWxInfo) TableName() string {
	return "UserWxInfo"
}

// 写入数据库
func (this *UserWxInfo) WriteToDB() {
	opr := dbhelper.GetMysqlHelper().GetNewOrm()
	findData := UserWxInfo{OpenId: this.OpenId}
	err := opr.Read(&findData, "OpenId")
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		// 新增
		_, errI := opr.Insert(this)
		if errI != nil {
			logs.Error("Insert UserWxInfo err :" + errI.Error())
		}
	}
}

// 查询上级
func (this *UserWxInfo) FindUserWxInfo() bool {
	opr := dbhelper.GetMysqlHelper().GetNewOrm()
	findData := UserWxInfo{OpenId: this.OpenId}
	err := opr.Read(&findData, "UnionId")
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return false
	}
	this.UnionId = findData.UnionId
	this.Sex = findData.Sex
	this.NickName = findData.NickName
	this.HeadImgUrl = findData.HeadImgUrl
	return true
}

// 资产变更金币
type RecoderGold struct {
	Id       		int64
	UnionId			string 	`orm:"column(UnionId)"`									//	账户ID
	OpenId			string 	`orm:"column(OpenId)"`									//	Open ID
	NickName		string 	`orm:"column(NickName)"`								//  昵称
	Change 			int64 	`orm:"column(Change)"`									//	变动数值
	Remind 			int64 	`orm:"column(Remind)"`									// 	剩余
	Desc			string 	`orm:"column(Desc),null"`								//  描述
	RecoderTime     time.Time `orm:"auto_now_add;type(datetime)"`					//  记录时间
}

func (this *RecoderGold) TableName() string {
	return "RecoderGold"
}

// 写入数据库
func (this *RecoderGold) WriteToDB() {
	opr := dbhelper.GetMysqlHelper().GetNewOrm()
	_, errI := opr.Insert(this)
	if errI != nil {
		logs.Error("Insert RecoderGold err :" + errI.Error())
	}
}

// 资产变更星星
type RecoderStars struct {
	Id       		int64
	UnionId			string 	`orm:"column(UnionId)"`									//	账户ID
	OpenId			string 	`orm:"column(OpenId)"`									//	Open ID
	NickName		string 	`orm:"column(NickName)"`								//  昵称
	Change 			int64 	`orm:"column(Change)"`									//	变动数值
	Remind 			int64 	`orm:"column(Remind)"`									// 	剩余
	Desc			string 	`orm:"column(Desc)"`									//  描述
	RecoderTime     time.Time `orm:"auto_now_add;type(datetime)"`					//  记录时间
}

func (this *RecoderStars) TableName() string {
	return "RecoderStars"
}

// 写入数据库
func (this *RecoderStars) WriteToDB() {
	opr := dbhelper.GetMysqlHelper().GetNewOrm()
	_, errI := opr.Insert(this)
	if errI != nil {
		logs.Error("Insert RecoderStars err :" + errI.Error())
	}
}